package com.ecar.api.support.util;

/**
 * 根据经纬度和半径，获取最大经纬度
 * 
 * @author caimf
 *
 */
public class LatLonUtil {
	private static final double PI = 3.14159265;
	private static final double EARTH_RADIUS = 6378137;
	private static final double RAD = Math.PI / 180.0;

	/**
	 * 
	 * @param lon
	 *            经度
	 * @param lat
	 *            纬度
	 * @param raidus 1000
	 *            距离 米
	 * @return 已该点为中心，raidus为半径的最大经纬度
	 * 最终返回的数据是，1414米，
	 * 返回数据依次为最小经度，最小纬度，最大经度，最大纬度
	 */
	public static double[] getAround(double lon, double lat, int raidus) {

		Double latitude = lat;
		Double longitude = lon;

		Double degree = (24901 * 1609) / 360.0;
		double raidusMile = raidus;

		Double dpmLat = 1 / degree;
		Double radiusLat = dpmLat * raidusMile;
		Double minLat = latitude - radiusLat;
		Double maxLat = latitude + radiusLat;

		Double mpdLng = degree * Math.cos(latitude * (PI / 180));
		Double dpmLng = 1 / mpdLng;
		Double radiusLng = dpmLng * raidusMile;
		Double minLng = longitude - radiusLng;
		Double maxLng = longitude + radiusLng;
		return new double[] { minLng, minLat, maxLng, maxLat };
	}

	public static double getDistance(double lng1, double lat1, double lng2, double lat2) {
		double radLat1 = lat1 * RAD;
		double radLat2 = lat2 * RAD;
		double a = radLat1 - radLat2;
		double b = (lng1 - lng2) * RAD;
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1)
				* Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		//System.out.println(s);
		return s;
	}

	public static void main(String[] args) throws Exception {
		double lat = 30.373441;
		double lon = 114.343304;
		int raidus = 1000;
		getAround(lon, lat, raidus);
		System.out.println(getDistance(114.343304, 30.373441, 114.33288932710057, 30.364455760277923));
	}

}
